package cn.laixueit.controller;

import cn.laixueit.pojo.Brand;
import cn.laixueit.result.Result;
import cn.laixueit.service.BrandService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/brand")
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/findall")
    public @ResponseBody Result findall(){
        List<Brand> brandList =  brandService.list();
        return new Result(200,brandList);
    }

    @GetMapping("/list")
    public @ResponseBody Result list(@RequestParam(defaultValue = "2") Integer pagesize,@RequestParam(defaultValue = "1") Integer page, String name){
        PageInfo<Brand> pageInfo =  brandService.list(pagesize,page,name);
        return new Result(200,pageInfo);
    }

    @PostMapping("/save")
    public @ResponseBody Result save(@RequestBody Brand brand){
        try {
            brandService.save(brand);
            return new Result(200,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(400,null);
    }

    @PostMapping("/update")
    public @ResponseBody Result update(@RequestBody Brand brand){
        brandService.update(brand);
        return new Result();
    }

    @GetMapping("/delById/{id}")
    public @ResponseBody Result delById(@PathVariable("id") Integer id){
        brandService.delById(id);
        return new Result();
    }

    @GetMapping("/findById/{id}")
    public @ResponseBody Result findById(@PathVariable("id") Integer id){
        Brand brand = brandService.findById(id);
        return new Result(200,brand);
    }

}
